var app = getApp();

Page({
  data: {
    selected: true,
    selected1: false,
    p: 1,
    tripList: [],
    isPullDown: false,
    isPullUp: false,
    isLoading: false,
    isPullUpText: '',
    page: 1,
    tripListEnd: [],
    isPullDown2: false,
    isPullUp2: false,
    isLoading2: false,
    isPullUpText2: ''
  },
  onShow: function() {
    var that = this;
    that.resetData();
    that.getList();
    that.getListEnd();
  },
  selected: function(e) {
    this.setData({
      selected1: false,
      selected: true,
      isPullDown: false,
      isPullUp: false,
      isLoading: false,
      isPullUpText: ''
    })
  },
  selected1: function(e) {
    this.setData({
      selected: false,
      selected1: true,
      isPullDown2: false,
      isPullUp2: false,
      isLoading2: false,
      isPullUpText2: ''
    })
  },
  onPullDownRefresh: function() {
    var that = this;
    if (that.data.selected) {
      if (that.data.isPullUp || that.data.isPullDown) {
        return
      }
      that.setData({
        p: 1,
        tripList: [],
        isPullDown: true,
        isPullUp: false,
        isLoading: false,
        isPullUpText: ''
      });
      that.getList();
    } else {
      if (that.data.isPullUp2 || that.data.isPullDown2) {
        return
      }
      that.setData({
        page: 1,
        tripListEnd: [],
        isPullDown2: true,
        isPullUp2: false,
        isLoading2: false,
        isPullUpText2: ''
      });
      that.getListEnd();
    }
    wx.stopPullDownRefresh();
  },
  onReachBottom: function() {
    var that = this;
    if (that.data.selected) {
      if (that.data.isPullUp || that.data.isPullDown) {
        return
      }
      that.setData({
        isPullUp: false,
        isPullDown: true,
        isLoading: true
      })
      that.getList();
    } else {
      if (that.data.isPullUp2 || that.data.isPullDown2) {
        return
      }
      that.setData({
        isPullUp2: false,
        isPullDown2: true,
        isLoading2: true
      })
      that.getListEnd();
    }
  },
  getList: function() {
    var that = this;
    app.JsonPost("Api/Trip/myList", {
      p: that.data.p
    }, function(res) {
      if (res.list.length > 0) {
        var l = that.data.tripList;
        for (var i = 0; i < res.list.length; i++) {
          res.list[i]["add_time"] = app.formatTime(res.list[i]["add_time"], 'Y-M-D h:m');
          l.push(res.list[i]);
        }
        that.setData({
          tripList: l,
          p: that.data.p + 1,
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '上拉加载更多',
        });
      } else {
        that.setData({
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '没有更多数据了'
        });
      }
    })
  },
  getListEnd: function(that) {
    var that = this;
    app.JsonPost("Api/Trip/myListEnd", {
      p: that.data.page
    }, function(res) {
      if (res.list.length > 0) {
        var list = that.data.tripListEnd;
        for (var i = 0; i < res.list.length; i++) {
          res.list[i]["add_time"] = app.formatTime(res.list[i]["add_time"], 'Y-M-D h:m');
          list.push(res.list[i]);
        }
        that.setData({
          tripListEnd: list,
          page: that.data.page + 1,
          isPullUp2: false,
          isPullDown2: false,
          isPullUpText2: '上拉加载更多',
        });
      } else {
        that.setData({
          isPullUp2: false,
          isPullDown2: false,
          isPullUpText2: '没有更多数据了'
        });
      }
    })
  },
  resetData: function() {
    var that = this;
    that.setData({
      p: 1,
      tripList: [],
      isPullDown: false,
      isPullUp: false,
      isLoading: false,
      isPullUpText: '',
      page: 1,
      tripListEnd: [],
      isPullDown2: false,
      isPullUp2: false,
      isLoading2: false,
      isPullUpText2: ''
    })
  },
  tripDetail: function(e) {
    var tripid = e.currentTarget.id;
    wx.navigateTo({
      url: 'tripDetail?tripId=' + tripid
    })
  },
  changeStatus: function(e) {
    var status = e.currentTarget.dataset.type;
    var tripId = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '点击“确定”将改变该行程的状态',
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          var jsonData = {
            id: tripId,
            state: status
          };
          app.JsonPost("Api/Trip/changeStatus", jsonData, function(res) {
            //console.log(res);
            that.onShow();
          })
        }
      }
    });
  },
  deleteTrip: function(e) {
    var tripId = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '点击“确定”将删除该行程',
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          app.JsonPost("Api/Trip/delete", {
            id: tripId
          }, function(res) {
            //console.log(res);
            that.onShow();
          })
        }
      }
    });
  }
})